

package linkeddataweb.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import linkeddataweb.professor.Professor;
import linkeddataweb.professor.Search;
import linkeddataweb.sparql.Sparql;

public class SearchServlet extends HttpServlet {

	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		
		Search s = new Search();
		
		s.setFilter(request.getParameter("type"));
		s.setSearch(request.getParameter("search"));
		
		try {
			if (request.getParameter("sendButton") != null) {
				
				Sparql query = new Sparql("C:\\mydataset", s.getSearch(), s.getFilter());
				PrintWriter out = response.getWriter();
				
				HttpSession session = request.getSession(true);
				ArrayList<Professor> p = query.getArrayProf();

				session.setAttribute("professor", p);
				
				
				if(request.getParameter("type").equals("query")){
					out.println("<script>	window.location.href = 'ListResearcherName.jsp'; </script>");
				}				
				
				out.println("<script>	window.location.href = 'ResearchProfile.jsp'; </script>");
					
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
		}

	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		Sparql query = new Sparql("C:\\mydataset", request.getParameter("professorName"));
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession(true);
		ArrayList<Professor> p = new ArrayList<Professor>();
		Professor prof = query.queryName(request.getParameter("professorName"));
		p.add(prof);
		
		session.setAttribute("professor", p);
		
		out.println("<script>	window.location.href = 'ResearchProfile.jsp'; </script>");
		
	}
	

}

